﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
namespace WebApplication1
{
    public partial class AddStaff : System.Web.UI.Page
    {
        DateTime teachFrmDate;
        DateTime teachToDate;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnStaffrAdd_Click(object sender, EventArgs e)
        {
            teachFrmDate = DateTime.MinValue;
            teachToDate = DateTime.MinValue;
            int startYear = 0;
            int endYear = 0;

            bool convertableToTeachFrmDate = DateTime.TryParse(txtStaffTeachFrm.Text.ToString(), out teachFrmDate);
            bool convertableToTeachToDate = DateTime.TryParse(txtStaffTeachTo.Text.ToString(), out teachToDate);

            bool convertabeToIntegerTeachFrmYr = false;
            bool convertabeToIntegerTeachToYr = false;

            if (!convertableToTeachFrmDate)
            {
                convertabeToIntegerTeachFrmYr = Int32.TryParse(txtStaffTeachFrm.Text.ToString(), out startYear);
                if (convertabeToIntegerTeachFrmYr && startYear.ToString().Length == 4)
                {
                    teachFrmDate = Convert.ToDateTime(startYear + "-01-01");

                }
                else
                {

                    //Response.Redirect("AddCommittee.aspx?Entered Committee Start Year is not in valid format");
                    lblAddStaff.Text = "Entered Staff's 'Teach From' is not in valid format";
                    return;
                }
            }

            if (!convertableToTeachToDate)
            {

                convertabeToIntegerTeachToYr = Int32.TryParse(txtStaffTeachTo.Text.ToString(), out endYear);

                if (convertabeToIntegerTeachToYr && endYear.ToString().Length == 4)
                {
                    //If the enter the same end yr as start yr thn keep as it's otherwise reduce one yr eg : start 01-01-1990 end 31-12-1991
                    if (startYear == endYear)
                    {
                        teachToDate = Convert.ToDateTime(endYear + "-12-31");
                    }
                   else if ((endYear - startYear) > 0)
                    {
                        teachToDate = Convert.ToDateTime(endYear - 1 + "-12-31");
                    }
                    else
                    {
                       // Response.Redirect("AddCommittee.aspx?Entered Committee Start Year & End Year are too differ");
                        lblAddStaff.Text = "Entered staff's  Teach to date is less than start year";
                        return;
                    }

                }
                else
                {
                    if(chkStaffIsCurrentStaff.Checked)
                    {
                        teachToDate = DateTime.MaxValue;
                    }
                    else
                    {
                    //Response.Redirect("AddCommittee.aspx?Entered Committee End Year is not in valid format");
                    lblAddStaff.Text = "Entered staff's  Teach to date is not in valid format";
                    return;
                    }

                }

            }



            try
            {
                StaffDTO staffDTO = new StaffDTO
                {
                    StaffId = Guid.NewGuid(),
                    FirstName = txtStaffFirstName.Text,
                    LastName = txtStaffLastName.Text,
                    //if he ddnt enter bday jst giv 1800 
                    DateOfBirth = txtStaffDOB.Text == "" ? Convert.ToDateTime("1800-01-01"): Convert.ToDateTime(txtStaffDOB.Text),
                    Role = ddlStaffRole.SelectedValue,
                    Address = txtStaffPermAddress.Text,
                    PhoneNumber = txtStaffTelephoneNumber.Text,
                    Email = txtStaffEmail.Text,
                    Subject = ddlStaffSubject.SelectedValue,
                    DegreesDetails = txtStaffDegree.Text,
                    TeachFrom = teachFrmDate,
                    IsCurrentStaff = chkStaffIsCurrentStaff.Checked,
                    //If he is a current staff then pass the data as max date as if you insert curr date then it wont gt updated
                    TeachTo = chkStaffIsCurrentStaff.Checked == true ? DateTime.MaxValue : teachToDate,
                    Description = txtStaffDesc.Text,
                    PhotoPath = fuStaffPhoto.FileName,
                    //ModifiedBy = "Tharsan",
                    ModifiedTime = DateTime.Now
                };
                AdminAL adminAL = new AdminAL();
                string result = adminAL.saveStaff(staffDTO);

                if (result == "Saved")
                {
                    if ((fuStaffPhoto.PostedFile != null) && (fuStaffPhoto.PostedFile.ContentLength > 0))
                    {
                        string fileName = System.IO.Path.GetFileName(fuStaffPhoto.PostedFile.FileName);
                        string saveLocation = Server.MapPath("staffPhotos") + "\\" + fileName;
                        try
                        {
                            fuStaffPhoto.PostedFile.SaveAs(saveLocation);
                            Response.Write("The File has been uploaded successfully");
                        }
                        catch (Exception ex)
                        {
                            Response.Write("Error occured while uploading the files...!!!" + ex);
                        }
                    }
                    else
                    {
                        Response.Write("Please select a file to upload.");

                    }

                    lblAddStaff.Text = "The Staff details have been saved successfully";
                }

                else if (result == "Updated")
                {
                    lblAddStaff.Text = "The Staff details have been Updated successfully";
                }

                else if (result == "AlreadyExists")
                {
                    lblAddStaff.Text = "The Staff details added by you is already exists with this name";
                }
                else if (result == "ConflictingPrincipalPeriod")
                {
                    lblAddStaff.Text = "There is already a pricipal exists with this duty period";
                }
                else if (result == "currentPrincipalsExceeded")
                {
                    lblAddStaff.Text = "There is already a pricipal working at present, hence if you want to add new principal, then update this principal to IsCurrentStaff 'false'";
                }
                else
                {
                    lblAddStaff.Text = "Due to some errors  your data is not added to our web site";
                }
            }
            catch (Exception ee)
            {
                lblAddStaff.Text = "There are some exceptions while adding your details." + ee.Message;
            }
            

            
        }

        

        
    }
}
